package ee.tlnk.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ee.tlnk.model.AccountModel;
import ee.tlnk.service.AccountService;

/**
 * The Class AccountController.
 */
@Controller
public class AccountController {

	/** The account service. */
	@Autowired
	private  AccountService  accountService;

	/**
	 * List account.
	 *
	 * @param map the map
	 * @return the string
	 */
	@RequestMapping("/account")
	public String listAccount(Map<String, Object> map) {

		map.put("account", new AccountModel());
		map.put("accountList", accountService.listAccount());

		return "account";
	}
	

	/**
	 * Adds the account.
	 *
	 * @param account the account
	 * @param result the result
	 * @return the string
	 */
	@RequestMapping(value = "/account/add", method = RequestMethod.POST)
	public String addAccount(@ModelAttribute("account") AccountModel account,
			BindingResult result) {

		accountService.addAccount(account);

		return "redirect:/account";
	}

	/**
	 * Delete account.
	 *
	 * @param accountId the account id
	 * @return the string
	 */
	@RequestMapping("/account/delete/{accountId}")
	public String deleteAccount(@PathVariable("accountId") Integer accountId) {

		accountService.removeAccount(accountId);

		return "redirect:/account";
	}
}
